package cn.huobi.basic.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.context.ApplicationContext;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.io.File;
import java.io.FileWriter;


public class FreemarkUtil {

    public static void createFreemark(String model, Object object, String path, Long fileName){
        FileWriter fileWriter =  null;
        try {
            ApplicationContext context = SpringUtil.getContext();
            FreeMarkerConfigurer freeMarkerConfigurer = context.getBean(FreeMarkerConfigurer.class);
            // 1.创建模板技术的核心配置对象
            Configuration configuration = freeMarkerConfigurer.getConfiguration();
            // 2.指定模板名称
            Template template = configuration.getTemplate(model);
            // 3.准备数据
            //Pet pet = petService.getPetDetail(12L);

            fileWriter = new FileWriter(new File(path + "\\" + fileName + ".html"));
            // 4.根据数据+模板在指定位置生成静态页面
            template.process(object, fileWriter);
        }catch (Exception e){
           e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
            } catch (Exception e){
                e.printStackTrace();
            }

        }

    }
}
